<?php
// +----------------------------------------------------------------------
// | 个人开发临时程序页面
// +----------------------------------------------------------------------
// | Author: TongBC <50249101@qq.com>
// +----------------------------------------------------------------------
// | 请勿乱动
// +----------------------------------------------------------------------
include_once '/var/www/html/new/com.inc.php';
include_once '/var/www/html/new/d/common/func.inc.php';
include_once '/var/www/html/new/d/class/abc/Product.class.php';

exit;
if(!isset($_SESSION['sid'])) 
	exit('<script>alert("登陆过期");window.close();</script>');

if(($config_id=($_REQUEST['did'] + 0))==0) 
	exit('<script>alert("参数错误");window.close();</script>');
// exit;
$memberSID = $_SESSION['sid'];
// $sample_id = 6889;  // 三亚先行分销商样本 7794
$sample_id = 7794;  // 三亚先行分销商样本 7794

$ac = $GLOBALS['_ac'];
$pw = $GLOBALS['_pw'];
$s  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));


// 获取供应商已开启转供应的产品
$today = date('Y-m-d');
$sql = "select e.id,e.fid,e.pid,e.sid,e.aids,e.lvl,sourceid from pft_p_apply_evolute e inner join uu_products p on p.id=e.pid inner join uu_jq_ticket t on t.pid=p.id where e.fid=$memberSID and p.apply_limit=1 and p.p_status in(0,2,3,4,5) and e.status=0 and t.pay<>0 and e.active=1";
$GLOBALS['le']->query($sql);$prods = array();
while($row=$GLOBALS['le']->fetch_assoc()){
	$price = abc777\Product::Price($s, $row['pid'], $row['sid'], $today);
	if($price['js']==-1) continue;
	$row['js'] = $price['js'];
	$prods[$row['pid']] = $row;
}

// 获取配置样本可购买的产品权限
$pids   = implode(',', array_keys($prods)); $sample_edatas = array();
$sql = "select id,pid from pft_p_apply_evolute where fid=$sample_id and sid=$memberSID and status=0 and pid in($pids)";
$GLOBALS['le']->query($sql);
while($row = $GLOBALS['le']->fetch_assoc()) $sample_edatas[$row['pid']] = $row;
$sample_edatas_diff = array_intersect_key($prods, $sample_edatas);// 获取交集即获取样本可销售的产品


// 获取样本可销售的产品的差价  
$sale_pids = implode(',', array_keys($sample_edatas_diff)); $sample_dprice = array();
if($sale_pids=='')  exit('<script>alert("样本产品");window.close();</script>');
$sql = "select dprice,aid,pid,tid from uu_priceset where pid=$sample_id and aid=$memberSID and tid in ($sale_pids)";
$GLOBALS['le']->query($sql);
while($row=$GLOBALS['le']->fetch_assoc()) $sample_dprice[$row['tid']] = $row;

// 500个会员配置一次
$pageStart = 0;$pageSize = 10; $did_arr = array();
$sql = "select memberID from pft_member_card where apply_did=$memberSID and status=0 order by id asc limit $pageStart,$pageSize"; 
$GLOBALS['le']->query($sql);
while($row=$GLOBALS['le']->fetch_assoc()) $did_arr[] = $row;

foreach($did_arr as $did){
	
	$config_id = $did['memberID'];
	
	// 配置获取产品权限
	$sale_edatas = array(); $upd_arr = array();
	$sql = "select id,status,pid from pft_p_apply_evolute where fid=$config_id and sid=$memberSID and pid in($sale_pids)";
	$GLOBALS['le']->query($sql);
	while($row = $GLOBALS['le']->fetch_assoc()){
		$sale_edatas[$row['pid']] = $row;
		if($row['status']==0) continue;
		$sql = "update pft_p_apply_evolute set status=0 where id={$row['id']} and fid=$config_id and sid=$memberSID limit 1";
		$upd_arr[] = $sql;
	}
	
	foreach($upd_arr as $key=>$sql){
		echo $sql;
		// $GLOBALS['le']->query($sql);
	}
	
	// 未配置没有记录的产品
	$sale_edatas_diff = array_diff_key($sample_edatas_diff, $sale_edatas);
	if(count($sale_edatas_diff)){
		$ins = "insert into pft_p_apply_evolute (sid,fid,sourceid,pid,aids,lvl,rectime) values ";
		foreach($sale_edatas_diff as $key=>$row){
			$aid_new = $row['aids'].','.$memberSID;
			$lvl_new = $row['lvl']+1;
			$ins .= "($memberSID,$config_id,{$row['sourceid']},{$row['pid']},'$aid_new',$lvl_new,now()),";
		}
		$ins = rtrim($ins, ',');
		// $GLOBALS['le']->query($ins);
		echo $ins;
	}

	// 分销差价配置
	$sale_dprice = array(); $upd_arr = array();
	$sql = "select id,dprice,aid,pid,tid from uu_priceset where pid=$config_id and aid=$memberSID and tid in ($sale_pids)";
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$sale_dprice[$row['tid']] = $row;
		if($sample_dprice[$row['tid']]['dprice']==$row['dprice']) continue;
		$dprice = $sample_dprice[$row['tid']]['dprice'];// 差价不一致以样本为准
		if($dprice<0) $dprice = 0;
		$sql = "update uu_priceset set dprice=$dprice where id={$row['id']} and pid=$config_id and aid=$memberSID limit 1";
		$upd_arr[] = $sql;
	}
	
	foreach($upd_arr as $key=>$sql){
		echo $sql;
		// $GLOBALS['le']->query($sql);
	}

	$dprice_diff = array_diff_key($sample_dprice, $sale_dprice);
	if(count($dprice_diff)){
		$ins = "insert into uu_priceset (tid,pid,aid,dprice) values ";
		foreach($dprice_diff as $key=>$row){
			$ins .= "({$row['tid']},$config_id, $memberSID,{$row['dprice']}),";
		}
		$ins = rtrim($ins, ',');
		// $GLOBALS['le']->query($ins);
		echo $ins;
	}
}
		

// exit('<script>alert("配置成功");window.close();</script>');